<?php
/**
 * Modello PROVINCE - LaFabbrica.org
 *
 * Le funzioni principali, fetchAll() ecc. sono generate da Classes_DbTable_Table
 * 
 * @author luigi.pastore
 * @created  30-mag-2011
 */
class Classes_Province extends Classes_DbTable_Table
{

    /**
     * Mapping al table model
     */
    protected $_table	= 'fab_provinces';
    protected $_primary = 'province_id';

    public function fetchById( $id )
    {
	if( !is_numeric( $id ) )
	    throw new Classes_Exceptions( 'ProvinceId Invalido: '. $id );
	
	$sql_data = array( 0 => $this->_table, // tabella
            1 => array( '*' ), //
            2 => array( 'province_id' => $id ), // where
            3 => array( 'fab_regions' => array( 'fab_regions.region_id', 'fab_provinces.province_region_id' ) ) // Left Join
        );

        $result = $this->db->simpleSelectQuery( $sql_data );

	if( $result->num_rows > 0 )
	{
	    return $result->fetch_assoc();	
	}
    }

    /**
     * Aggiunge parametri Join e OrderBy, gli altri vengono gestiti da 
     * Classes_DbTable_Table
     * 
     * @param array $params
     * @return array 
     */
    public function fetchAll( array $params = array() )
    {
	$sql_data = array(
            'join' => array( 'fab_regions' => array( 'fab_regions.region_id', 'fab_provinces.province_region_id' ) ),
	    'orderby' => array( 'province_long_name' => 'ASC' )
        );

	return parent::fetchAll( $sql_data );
    }

}